import { createRouter, createWebHistory } from 'vue-router'
import Login from "@/views/Login/Login";
import Home from "@/views/Index/Home";
const routes = [
  {path: '/',redirect:"/login"},
  {path: '/login',name: 'login',component: Login},
  {path: '/home',name: 'home',redirect:"/welcome",component: Home,children:[
      {path: '/welcome', name: "welcome", component: ()=>import("@/views/welcome/Welcome")},
      {path: '/userlist', name: "userlist", component: ()=>import("@/views/Users/Users")},
    ]},
  {path: '/:catchAll(.*)', name: '404', component: ()=>import("@/views/404/404")},

]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
// 导航守卫,未登录的用户不可访问login页面以外的其他页面
router.beforeEach((to,from,next)=>{
  if(to.path === "/login"){return next()}
  const tokenStr = window.sessionStorage.getItem("ass_token")
  if(!tokenStr){return next('/login')}
  next()
})
export default router
